package com.zx.oa.controller;

import com.zx.oa.entity.ClassInfo;
import com.zx.oa.entity.Major;
import com.zx.oa.entity.ShowStudent;
import com.zx.oa.service.ClassInfo.ClassInfoService;
import com.zx.oa.service.ClassInfo.ClassInfoServiceImpl;
import com.zx.oa.service.major.MajorService;
import com.zx.oa.service.major.MajorServiceImpl;
import com.zx.oa.service.student.StudentService;
import com.zx.oa.service.student.StudentServiceImpl;
import com.zx.oa.utils.PageSupport;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/chaservlet.do")
public class Chaservlet extends HttpServlet {

    StudentService studentService=new StudentServiceImpl();
    MajorService majorService=new MajorServiceImpl();

    ClassInfoService classInfoServlet=new ClassInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=null;
        String className=null;
        String major=null;
        String queryName=request.getParameter("namec");
        String queryClass=request.getParameter("classname");
        String queryMajor=request.getParameter("major");
        if(null!=queryName&&!"".equals(queryName)){
            name=queryName;
        }
        if(null!=queryClass&&!"".equals(queryClass)){
            className=queryClass;
        }
        if(null!=queryMajor&&!"".equals(queryMajor)){
            major=queryMajor;
        }
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        //获取学生信息
        List<ShowStudent> showStudentList=studentService.getStudentList(name,className,major,currentPageNo,pageSize);

        //获取到总记录数
        int totalCount= studentService.getStudentTotalCount(name,className,major);

        //取到所有专业的列表
        List<Major> majorList=majorService.getMajorAll();
        //取到所有班级的列表
        List<ClassInfo> classInfoList=classInfoServlet.getClassInfo(null);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);

        //获取所有学生信息
        List<ShowStudent> studentList=studentService.getStudentListAll(name,className,major);

        request.setAttribute("pageInfo",pages);//分页信息
        request.getSession().setAttribute("studentList",showStudentList);
        request.getSession().setAttribute("studentListAll",studentList);
        request.getSession().setAttribute("majors",majorList);
        request.getSession().setAttribute("cList",classInfoList);
        request.setAttribute("name",name);
        request.getRequestDispatcher("jsp/showStudent.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
